package org.freedoit.server.service;

import java.sql.Timestamp;

import org.freedoit.common.util.SharedConstants;
import org.freedoit.server.dao.DAOFactory;
import org.freedoit.server.dao.TaskDAO;
import org.freedoit.server.om.Task;
import org.freedoit.server.om.User;

public class ServiceManager {
	
	public static boolean setTaskFinished(Long taskId, Long clientId, Boolean isFinished, Long timestamp) {
		TaskDAO taskDAO = DAOFactory.getTaskDAO();
		Task task = taskDAO.findById(taskId);
		
		if (task != null) {
			task.setFinished(isFinished);
			task.setLastModifyTime(new Timestamp(timestamp));
			taskDAO.update(task);
			return true;
		} else {
			return false;
		}
	}

	public static boolean addNewTask(Task newTask, Long userId, Long clientId, Long projectId, 
			Long taskTypeId, Long timestamp) {
		// Get the information of the user
		User user = null;
		try {
			user = DAOFactory.getUserDAO().findById(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (user == null)
			return false;
		System.out.println(userId);
		
		newTask.setUserId(userId);
		newTask.setLastModifyByClientId(clientId);
		
		if (clientId == null)
			return false;
		
		// If this task belongs to one project
		if (projectId != null) {
			newTask.setProjectId(projectId);
		}
		
		// If this task belongs to one task type
		if (taskTypeId != null) {
			newTask.setTaskTypeId(taskTypeId);
		}
		
		// Final save the task by adding default settings
		newTask.setLastModifyTime(new Timestamp(timestamp));
		newTask.setStatus(SharedConstants.ACTIVE_INTEGER);
		newTask.setLastModifyByClientId(clientId);
		
		try {
			DAOFactory.getTaskDAO().persist(newTask);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return true;
		
	}

	public static boolean updateTask(Task taskToUpdate, Long clientId, Timestamp timestamp) {
		Task existingTask = DAOFactory.getTaskDAO().findById(taskToUpdate.getTaskId());
		if (existingTask != null) {
			existingTask.setLastModifyTime(timestamp);
			
			// Update information
			existingTask.setDescription(taskToUpdate.getDescription());
			existingTask.setTaskTypeId(taskToUpdate.getTaskTypeId());
			existingTask.setProjectId(taskToUpdate.getProjectId());
			existingTask.setTaskName(taskToUpdate.getTaskName());
			existingTask.setStartDate(taskToUpdate.getStartDate());
			existingTask.setDueDate(taskToUpdate.getDueDate());
			existingTask.setStartTime(taskToUpdate.getStartTime());
			existingTask.setDueTime(taskToUpdate.getDueTime());
			existingTask.setTaskTag(taskToUpdate.getTaskTag());
			
			existingTask.setLastModifyByClientId(clientId);
			
			DAOFactory.getTaskDAO().persist(existingTask);
			
			return true;
			
		} else {
			return false;
		}
	}

	public static boolean saveOrUpdateTask(Task taskToSaveOrUpdate, Long userId, Long clientId, Timestamp timestamp) {
		Long taskId = taskToSaveOrUpdate.getTaskId();
		Task existingTask = null;
		if (taskId != null) {
			existingTask = DAOFactory.getTaskDAO().findById(taskToSaveOrUpdate.getTaskId());
			if (existingTask != null) {
				
				// Update information
				existingTask.setDescription(taskToSaveOrUpdate.getDescription());
				existingTask.setTaskTypeId(taskToSaveOrUpdate.getTaskTypeId());
				existingTask.setProjectId(taskToSaveOrUpdate.getProjectId());
				existingTask.setTaskName(taskToSaveOrUpdate.getTaskName());
				existingTask.setStartDate(taskToSaveOrUpdate.getStartDate());
				existingTask.setDueDate(taskToSaveOrUpdate.getDueDate());
				existingTask.setStartTime(taskToSaveOrUpdate.getStartTime());
				existingTask.setDueTime(taskToSaveOrUpdate.getDueTime());
				existingTask.setTaskTag(taskToSaveOrUpdate.getTaskTag());
				
			} else { // not exists
				existingTask = taskToSaveOrUpdate;
				existingTask.setTaskId(null);
			}
		} else {
			existingTask = taskToSaveOrUpdate;
		}

		// common information to update
		existingTask.setLastModifyTime(timestamp);
		existingTask.setLastModifyByClientId(clientId);
		
		// save or update
		DAOFactory.getTaskDAO().saveOrUpdate(existingTask);
		
		return true;
	}

	public static boolean deleteTask(Long taskId, Long clientId,  Long timestamp) {
		TaskDAO taskDAO = DAOFactory.getTaskDAO();
		Task task = taskDAO.findById(taskId);
		
		if (task != null) {
			task.setStatus(SharedConstants.DELETED_INTEGER);
			task.setLastModifyTime(new Timestamp(timestamp));
			task.setLastModifyByClientId(clientId);
			taskDAO.update(task);
			return true;
		} else {
			return false;
		}
	}

}